﻿using Xejen.Hosting.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;
using Xejen.Languages;

namespace Xejen.Languages.Abstract
{
    /// <summary>
    /// 针对<see cref="ILanguageBuilder"/>进行扩展
    /// </summary>
    public static class LanguageBuilderExtensions
    {
        private readonly static LanguageConfig _config = new LanguageConfig();

        /// <summary>
        /// 配置多语言环境的参数
        /// </summary>
        /// <param name="builder"><inheritdoc cref="ILanguageBuilder"/></param>
        /// <param name="configure">配置行为</param>
        /// <returns></returns>
        public static ILanguageBuilder ConfigureLanguageConfiguration(this ILanguageBuilder builder, Action<LanguageConfig> configure)
        {
            var logger = builder.LoggerManager.CreateLogger("Xejen.Languages.Abstract.LanguageBuilderExtensions");

            builder.Configuration.Bind(_config.GetType().Name, _config);

            configure?.Invoke(_config);

            builder.Host.AddServices((services) =>
            {
                services.AddSingleton(_config);
            });

            logger.LogInformation($"完成{nameof(LanguageConfig)}配置：{{@value}}", _config);

            return builder;
        }

        /// <summary>
        /// 添加多语言环境模块
        /// </summary>
        /// <param name="builder"><inheritdoc cref="ILanguageBuilder"/></param>
        /// <returns></returns>
        public static ILanguageBuilder AddLanguage(this ILanguageBuilder builder)
        {
            var logger = builder.LoggerManager.CreateLogger("Xejen.Languages.Abstract.LanguageBuilderExtensions");
            builder.Host
                .AddServices((services) =>
                {
                    services.AddSingleton<ILanguageManager, LanguageManager>();
                });

            logger.LogInformation($"完成多语言模块的添加");

            return builder;
        }
    }
}
